﻿using Furion.DynamicApiController;
using Furion.UnifyResult;
using Microsoft.AspNetCore.Mvc;
using MSEAP.Entitys;
using Neo4jClient;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace MSEAP.Controllers
{
    [DynamicApiController]
    public class NeoController
    {
        private readonly ILogger<NeoController> logger;
        private readonly ISqlSugarClient db;
        private readonly IGraphClient neo;

        public NeoController(ILogger<NeoController> logger, ISqlSugarClient db, IGraphClient neo)
        {
            this.db = db;
            this.logger = logger;
            this.neo = neo;
        }

        public async Task<IActionResult> Index()
        {
            var data = await neo.Cypher.Match(@"(p:person)").Return((p) => p.As<Person>()).ResultsAsync;
            foreach(var d in data) {
                Console.WriteLine(JObject.FromObject(d));
            }
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = data,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        public async Task<IActionResult> List()
        {
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = db.DbMaintenance.GetTableInfoList(false),
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }
    }

    internal class Person
    {
        public long id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
    }
}
